<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>班级管理</span>
        <div class="extra">
          <el-button type="primary" @click="showDialog()">添加班级</el-button>
        </div>
      </div>
    </template>
    <el-table :data="clazzes" style="width: 100%">
      <el-table-column label="序号" width="50" type="index"></el-table-column>
      <el-table-column label="班级名" prop="clazzName"></el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">

          <el-button icon="el-icon-delete" circle plain type="danger" @click="deleteClazz(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="clazzModel" :rules="rules" label-width="100px">
        <el-form-item label="班级名" prop="clazzName">
          <el-input v-model="clazzModel.clazzName"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="title === '添加班级' ? addClazz() : updateClazz()">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { fetchClassList, addClass, deleteClassById } from '@/api/classService.js';

const clazzes = ref([]);
const dialogVisible = ref(false);
const clazzModel = ref({ clazzName: '' });
const title = ref('');
const rules = {
  clazzName: [{ required: true, message: '请输入班级名', trigger: 'blur' }]
};
const page = ref(1); // 当前页
const total = ref(0); // 总条数
const pageSize = ref(10); // 每页条数
const fetchClazzes = async () => {
  try {
    const result = await fetchClassList({
      clazzname: '',
      page: page.value,
      pageSize: pageSize.value
    });
    if (result.code === 0 && result.data && Array.isArray(result.data.records)) {
      clazzes.value = result.data.records;
    } else {
      ElMessage.error('未能正确获取班级列表数据');
    }
  } catch (error) {
    console.error('Fetch clazzes error:', error);
    ElMessage.error('获取班级列表失败');
  }
};

const addClazz = async () => {
  try {
    const result = await addClass(clazzModel.value.clazzName);
    ElMessage.success(result.msg || '添加成功');
    await fetchClazzes();
    dialogVisible.value = false;
  } catch (error) {
    ElMessage.error('添加班级失败');
  }
};

const updateClazz = async () => {
  // Implement update class logic if needed
};

const deleteClazz = (row) => {
  ElMessageBox.confirm('你确认要删除该班级吗?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteClassById(row.id);
      ElMessage.success('删除成功');
      await fetchClazzes();
    } catch (error) {
      ElMessage.error('删除班级失败');
    }
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};



const showDialog = (row = null) => {
  dialogVisible.value = true;
  title.value = row ? '编辑班级' : '添加班级';
  if (row) {
    clazzModel.value = { ...row };
  } else {
    clazzModel.value = { clazzName: '' };
  }
};

onMounted(() => {
  fetchClazzes();
});
</script>

<style scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
